package com.hxd.otc.utils;

import android.content.Context;

import com.hxd.otc.base.BaseApplication;

import java.io.File;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import top.zibin.luban.Luban;

/**
 * 包  名 :  com.hxd.otc.utils
 * 类  名 :  LubanUtils
 * 作  者 :  zjl
 * 时  间 :  2018/6/19 12:24
 * 描  述 :  ${TODO}
 */
public class LubanUtils {

    CompositeDisposable mDisposable;

    public void withRx(final Context activity, final List<String> photos, Consumer<List<File>> consumer) {
        mDisposable = new CompositeDisposable();
        mDisposable.add(Flowable.just(photos)
                .observeOn(Schedulers.io())
                .map(new Function<List<String>, List<File>>() {
                    @Override
                    public List<File> apply(@io.reactivex.annotations.NonNull List<String> list) throws Exception {
                        return Luban.with(BaseApplication.getInstance()).load(list).get();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(consumer));
    }

    public void clear() {
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
            mDisposable.clear();
            mDisposable = null;
        }
    }
}
